<?php

namespace app\admin\model;

use think\Model;
use think\Db;

/*
 * 美容师证书
 */

class StaffCert extends Model {
    /*
     * 生成证书号
     */

    public static function makerCertNum($rank, $first, $num) {
        $rankLength = 5;

        $length = strlen($num);
        if ($length == $rankLength) {
            $num = $num;
        } else if ($length < $rankLength) {
            $lastLength = $rankLength - $length;
            $string = '';
            for ($i = 0; $i < $lastLength; $i++) {
                $string = $string . '0';
            }
            $num = $string . $num;
        } else if ($length > $rankLength) {
            $num = substr($num, -5);
        }
        $rankCode = StaffRank::where('id', $rank)->value('code');
        $certNum = '10' . substr(date('Y'), -2) . date('m') . strtoupper($rankCode) . $first . $num;
        $coderow = Db::name('staff_cert')
                ->where('cert_num', '=', $certNum)
                ->find();
        if (is_null($coderow)) {
            return $certNum;
        } else {
            self::makerCertNum($rank, $first, $num);
        }
    }

}
